package com.feng.jvm.test;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.text.MessageFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author LEE
 */
public class Other1 {


    @Test
    public void test10(){
        HashMap<String, byte[]> map = new HashMap<>();
        IntStream.range(0,Integer.MAX_VALUE).forEach(i -> {
            try {
                map.put(UUID.randomUUID().toString(),new byte[1024]);
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.gc();
        });
    }

    @Test
    public void test9(){
        try {
            int i = 0 / 10;
        } finally {

        }
        System.out.println("test9");
    }

    @Test
    public void test8() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("test.ftl");
        URL url = classPathResource.getURL();
        System.out.println(url.toString());
        System.out.println(classPathResource.getURI().toString());
        URL resource = Other1.class.getClassLoader().getResource("test.ftl");
        System.out.println(url.toString());
    }

    @Test
    public void test7(){
        StringWriter stringWriter = new StringWriter();
        stringWriter.write("D:\\课程视频\\nio.zip");
        stringWriter.write("aaa",7,3);
        System.out.println(stringWriter.toString());
    }


    @Test
    public void test6(){
        m2();
        System.out.println("CCCC");
        System.out.println(Test3.Test3Inner.instance);
    }

    @Test
    public void test5(){
        try {
            String fileName = "D:\\work\\南商\\pdf合并\\OtherTest10.pdf";
            File src = new File("D:\\work\\南商\\pdf合并\\OtherTest10.pdf");
            Integer i = new Random().nextInt(Integer.MAX_VALUE);
            String format = MessageFormat.format("{0}{1}.{2}", FilenameUtils.removeExtension(fileName), i.toString(), FilenameUtils.getExtension(fileName));
            String x = FilenameUtils.removeExtension(fileName) + i + "." + FilenameUtils.getExtension(fileName);
            System.out.println(x);
            System.out.println(format);
            return;
        } catch (Exception e){

        }finally {
            System.out.println("AAAAA");
        }
    }

    public void m2(){
        try {
            System.out.println("AAAAA");
        } catch (Exception e) {

        }finally {
            System.out.println("BBBB");
        }
    }

    @Test
    public void test4() throws IOException {
        File src = new File("D:\\work\\南商\\pdf合并\\OtherTest10.pdf");
        File dest = new File("D:\\work\\南商\\pdf合并\\OtherTest11.pdf");
        File originFile = new File("D:\\work\\南商\\pdf合并\\OtherTest11.pdf");
        dest.deleteOnExit();
        FileUtils.copyFile(src,originFile);
    }

    @SneakyThrows
    @Test
    public void test3(){
        String dest = "D:\\work\\南商\\pdf合并\\OtherTest10.pdf";
        PdfReader reader = new PdfReader("D:\\work\\南商\\pdf合并\\BATCH20210602152205481.pdf");
        Rectangle pageSize = reader.getPageSize(1);
        Document document = new Document(pageSize);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfContentByte content = writer.getDirectContent();
        PdfImportedPage page = writer.getImportedPage(reader, 1);
// adding the same page 16 times with a different offset
        float x, y;
        for (int i = 0; i < 16; i++) {
//            x = -pageSize.getWidth() * (i % 4);
//            y = pageSize.getHeight() * (i/4 - 3);
            x = pageSize.getWidth()  * 0;
            y = pageSize.getHeight() * 0;
            content.addTemplate(page, 1, 0, 0, 1, x, y);
            System.out.println(MessageFormat.format("{0}:{1}",document.getPageSize(),i));
            document.newPage();
        }
        document.close();
        reader.close();
    }

    @SneakyThrows
    public void m1(String src,String dest){
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        int n = reader.getNumberOfPages();
        PdfDictionary page;
        PdfArray media;
        for (int p = 1; p <= n; p++) {
            page = reader.getPageN(p);
            media = page.getAsArray(PdfName.CROPBOX);
            if (media == null) {
                media = page.getAsArray(PdfName.MEDIABOX);
            }
            float llx = media.getAsNumber(0).floatValue() + 200;
            float lly = media.getAsNumber(1).floatValue() + 200;
            float w = media.getAsNumber(2).floatValue() - media.getAsNumber(0).floatValue() - 400;
            float h = media.getAsNumber(3).floatValue() - media.getAsNumber(1).floatValue() - 400;
            String command = String.format(
                    "\nq %.2f %.2f %.2f %.2f re W n\nq\n",
                    llx, lly, w, h);
            stamper.getUnderContent(p).setLiteral(command);
            stamper.getOverContent(p).setLiteral("\nQ\nQ\n");
        }
        stamper.close();
        reader.close();
    }

    @SneakyThrows
    @Test
    public void test1() {
        String dest = "D:\\work\\南商\\pdf合并\\OtherTest3.pdf";
        PdfReader pdfReader = new PdfReader("D:\\API\\spring-boot-reference.pdf");
        Document document = new Document(pdfReader.getPageSize(1), 0, 0, 0, 0);
        PdfCopy pdfCopy = new PdfCopy(document, new FileOutputStream(dest));
        document.open();
        int pages = pdfReader.getNumberOfPages();
        for (int i = 1; i <= pages; i++) {
            document.newPage();
            PdfImportedPage page = pdfCopy.getImportedPage(pdfReader, i);
            pdfCopy.addPage(page);
        }
        PdfContentByte pcb = pdfCopy.getDirectContent();
        document.close();
        pdfReader.close();
    }

    @Test
    public void test() throws IOException, DocumentException {
        String dest = "D:\\work\\南商\\pdf合并\\OtherTest12.pdf";
        PdfReader pdfReader = new PdfReader("D:\\work\\南商\\pdf合并\\BATCH20210602152205481.pdf.temp");
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfContentByte content = writer.getDirectContent();
        int pages = pdfReader.getNumberOfPages();
        for (int i = 1; i <= pages; i++) {
            if ((i % 2) != 0) {
                document.newPage();
            }
            PdfImportedPage page = writer.getImportedPage(pdfReader, i);
            if ((i % 2) != 0) {
                content.addTemplate(page,0,0);
            }
            else content.addTemplate(page, 0, -421);
        }
        document.close();
        pdfReader.close();
    }

    @SneakyThrows
    @Test
    public void test2(){
        List<String> files = new ArrayList<>();
        files.add("D:\\work\\南商\\pdf合并\\BATCH20210602152005280.pdf");
        files.add("D:\\work\\南商\\pdf合并\\BATCH20210602152205481.pdf");
        files.add("D:\\work\\南商\\pdf合并\\BATCH20210604142759210.pdf");
        ZipOutputStream zip =
                new ZipOutputStream(new FileOutputStream("D:\\work\\南商\\pdf合并\\zip.pdf"));
        for (int i = 1; i <= 3; i++) {
            ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");
            zip.putNextEntry(entry);

            // step 1
            Document document = new Document();
            // step 2
            PdfWriter writer = PdfWriter.getInstance(document, zip);
            writer.setCloseStream(false);
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph("Hello " + i));
            // step 5
            document.close();

            zip.closeEntry();
        }
        zip.close();

    }

}
